package kor.ketti.team;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

public class EditorActivity extends Activity implements OnClickListener {
	
	private static final String TAG = "EditorActivity";
	
	Context mContext;
	LayoutInflater inflater;
	
	AlertDialog dialog;		
	
	TextView a_edit,b_edit,c_edit;
	TextView slide_sourceView, menu;
	LinearLayout touch_listener, search_box, navigator;
	Button search_button;
	EditText source, search_edit;
	
	int backgroundColor_sourceSeparator;
	
	Button goto_button;
	
	boolean searchBoxAndNavigatorStatus = false;
	
	long touchDownTime = 0;
	long touchUpTime = 0;
	long touchTime = 0;
	
    /** Called when the activity is first created. */
    @Override
    
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_title);
        
        // get temp_handle
        TextView skip = (TextView)findViewById(R.id.skip);
        skip.setOnClickListener(this);
        
        // get resource color for background color
        backgroundColor_sourceSeparator = getResources().getColor(R.color.backgroundColor_sourceSeparator);
        
        /* for custom dialog, find_dialog & goto_dialog */
        {
        // step1:get context
		mContext = getApplicationContext();
		
		// step2:get inflater that return view
		inflater = (LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
				
        }
    }
    
    private void startEditor()
    {
    	setContentView(R.layout.view_source);
		
		// get handle of edit_view
		a_edit = (TextView)findViewById(R.id.a_source_view);
		a_edit.setOnClickListener(this);
		a_edit.setBackgroundColor(backgroundColor_sourceSeparator);
		
		b_edit = (TextView)findViewById(R.id.b_source_view);
		b_edit.setOnClickListener(this);
		
		c_edit = (TextView)findViewById(R.id.c_source_view);
		c_edit.setOnClickListener(this);
		
		menu = (TextView) findViewById(R.id.menu);
		
		source = (EditText) findViewById(R.id.source);
		source.setEnabled(false);
//		source.append(Html.fromHtml("<B><I><font color=red>public</font></I></B>"));
		search_edit = (EditText) findViewById(R.id.search_edit);
		search_button = (Button) findViewById(R.id.box_button);
		search_button.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				source.append(Html.fromHtml("<B><I><font color=red>public </font></I></B>"));
			}
		});
		
		search_box = (LinearLayout) findViewById(R.id.search_box);
		navigator = (LinearLayout) findViewById(R.id.navigator);
		
		slide_sourceView = (TextView) findViewById(R.id.slide_sourceView);
		slide_sourceView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Log.d(TAG, "slide has been touched");
				return true;
			}
		});
				
		touch_listener = (LinearLayout) findViewById(R.id.touch_listener);
		touch_listener.setOnTouchListener(new OnTouchListener() {
			int xpos = -1;
			int ypos = -1;
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				Log.d(TAG, Integer.toString(event.getPointerCount()));
				
				if(event.getPointerCount() > 1){
					int action = event.getAction();
					Log.d(TAG, "action : " + Integer.toString(action));
					int actionPointerId = action & MotionEvent.ACTION_POINTER_ID_MASK;
					int actionEvent = action & MotionEvent.ACTION_MASK;					
					
					Log.d(TAG, "actionPointerId : " + Integer.toString(actionPointerId));
					Log.d(TAG, "actionEvent : " + Integer.toString(actionEvent));

					xpos = (int) event.getX(0);
					ypos = (int) event.getY(0);
					Log.d(TAG, "x : " + xpos + ", " + "y : " + ypos);
					xpos = (int) event.getX(1);
					ypos = (int) event.getY(1);
					Log.d(TAG, "x : " + xpos + ", " + "y : " + ypos);
					
					if(actionEvent == MotionEvent.ACTION_POINTER_DOWN)
						touchDownTime = event.getEventTime();
					else if(actionEvent == MotionEvent.ACTION_POINTER_UP){
						touchUpTime = event.getEventTime();
						touchTime = touchUpTime - touchDownTime;
						Log.d(TAG, "touch time : " + Long.toString(touchTime));
						
						if(touchTime > 1000){						
							search_box.setVisibility(View.INVISIBLE);
							navigator.setVisibility(View.INVISIBLE);
							source.setEnabled(true);							
						}
						else{
							if(searchBoxAndNavigatorStatus == false){
								search_box.setVisibility(View.VISIBLE);
								navigator.setVisibility(View.VISIBLE);
								source.setEnabled(false);	
								search_edit.requestFocus();
								searchBoxAndNavigatorStatus = true;							
								return true;
								}
							else{
								search_box.setVisibility(View.INVISIBLE);
								navigator.setVisibility(View.INVISIBLE);
								searchBoxAndNavigatorStatus = false;
								source.setEnabled(false);							
								return true;
							}		
						}
					}
				}
				//touch_listener.setVisibility(View.INVISIBLE);
				return true;
			}
		});		
    }
    
	@Override
	public void onClick(View arg0)
	{
		// TODO Auto-generated method stub
		switch(arg0.getId())
		{
		case R.id.skip:
			startEditor();
	        break;
		case R.id.a_source_view:
			setTitle(R.string.app_name);
			setTitle(getTitle() + " - " + "a_source_filename");
			a_edit.setBackgroundColor(backgroundColor_sourceSeparator);
	        b_edit.setBackgroundColor(0);
	        c_edit.setBackgroundColor(0);
			break;
		case R.id.b_source_view:
			setTitle(R.string.app_name);
			setTitle(getTitle() + " - " + "b_source_filename");
			a_edit.setBackgroundColor(0);
			b_edit.setBackgroundColor(backgroundColor_sourceSeparator);
			c_edit.setBackgroundColor(0);
			break;
		case R.id.c_source_view:
			setTitle(R.string.app_name);
			setTitle(getTitle() + " - " + "c_source_filename");
			a_edit.setBackgroundColor(0);
			b_edit.setBackgroundColor(0);
			c_edit.setBackgroundColor(backgroundColor_sourceSeparator);
			break;
//		case R.id.go_button:
//			/* implement to goto line */			
//			//dialog.hide();
//			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		// TODO Auto-generated method stub
		MenuInflater inflater = this.getMenuInflater();
		inflater.inflate(R.menu.options_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		// TODO Auto-generated method stub
		switch(item.getItemId())
		{
		case R.id.Goto:
			// step3:get root-view that inflate custom-view on dialog
			View goto_layout = inflater.inflate(R.layout.dialog_goto,
					(ViewGroup) findViewById(R.id.goto_layout));
			
			// step4:build dialog
			AlertDialog.Builder bGoto = new AlertDialog.Builder(this);
			bGoto.setTitle("Goto");
			bGoto.setView(goto_layout);	
			bGoto.setPositiveButton("Go!!", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {					
					source.post(new Runnable() {
						
						@Override
						public void run() {					
							int i = source.getScrollY();
							Log.d(TAG, "scroll : " + Integer.toString(i));
							source.scrollTo(0, 0);							
						}
					});
					
				}
			});
			
			// step5:create dialog & show 
			dialog = bGoto.create();
			dialog.show();			
			return true;
		case R.id.undo:
			/* implement undo */
			return true;
		case R.id.annotation_1:
			/* implement to add '//' */
			return true;
		case R.id.annotation_2:
			/* implement to add '/*' */
			return true;
		}
		return false;
	}
    
}